<template>
  <div>
    <!-- 添加按钮 -->
    <div class="post-div">
      <el-button size="small" type="success" @click="addShow">添加</el-button>
    </div>
    <!-- 表格 -->
    <div>
      <el-table
        :data="menuList"
        style="width: 100%"
        row-key="id"
        border
        lazy
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column prop="title" label="菜单标题" />
        <el-table-column prop="component" label="路由名称" />
        <el-table-column prop="sortValue" label="排序" />
        <el-table-column prop="status" label="状态">
          <template #default="scope">
            <span>{{ +scope.row.status === 0 ? '禁用' : '正常' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column label="操作" align="center">
          <template #default="scope">
            <el-button
              size="small"
              type="success"
              @click="handleSave(scope.row)"
            >
              添加下级节点
            </el-button>
            <el-button
              size="small"
              type="primary"
              @click="handleEdit(scope.row)"
            >
              修改
            </el-button>
            <el-button
              size="small"
              type="danger"
              @click="handleDelete(scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 一级菜单添加弹窗 -->
    <div>
      <el-dialog v-model="dialogFormVisible" :title="title">
        <el-form :model="mainMenuForm">
          <el-form-item label="菜单标题" label-width="120px">
            <el-input v-model="mainMenuForm.title" autocomplete="off" />
          </el-form-item>

          <el-form-item label="路由名称" label-width="120px">
            <el-input v-model="mainMenuForm.component" autocomplete="off" />
          </el-form-item>

          <el-form-item label="排序" label-width="120px">
            <el-input v-model="mainMenuForm.sortValue" autocomplete="off" />
          </el-form-item>
          <el-form-item label="状态" label-width="120px">
            <el-radio-group v-model="mainMenuForm.status">
              <el-radio :label="1">正常</el-radio>
              <el-radio :label="0">停用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取消</el-button>
            <el-button type="primary" @click="handleSaveMenu">确认</el-button>
          </span>
        </template>
      </el-dialog>
    </div>
  </div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  GetMenuList,
  SaveFatherMenu,
  DeleteMenu,
  UpdateMenu,
} from '@/api/sysMenu.js'
const title = ref('添加')
const menuList = ref([])
const loadData = async () => {
  const { data } = await GetMenuList()
  menuList.value = data
}
onMounted(() => {
  loadData()
})
//添加父菜单弹框方法
const mainMenuForm = ref({})
const dialogFormVisible = ref(false)
const addShow = () => {
  mainMenuForm.value = {}
  dialogFormVisible.value = true
  title.value = '添加'
  mainMenuForm.value.parentId = 0
}
//添加子菜单弹框方法
const handleSave = row => {
  mainMenuForm.value = {}
  dialogFormVisible.value = true
  title.value = '添加下级节点'
  mainMenuForm.value.parentId = row.id
}
//修改弹框方法
const handleEdit = row => {
  Object.assign(mainMenuForm.value, row)
  dialogFormVisible.value = true
  title.value = '修改'
}
const handleSaveMenu = async () => {
  if (mainMenuForm.value.id) {
    //修改菜单
    await UpdateMenu(mainMenuForm.value)
    dialogFormVisible.value = false
    loadData()
    ElMessage.success('修改成功')
  } else {
    //添加菜单
    await SaveFatherMenu(mainMenuForm.value)
    dialogFormVisible.value = false
    loadData()
    ElMessage.success('添加成功')
  }
}

//删除
const handleDelete = row => {
  ElMessageBox.confirm(`确认删除【${row.title}】菜单吗?`, 'Warning', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(async () => {
      await DeleteMenu(row.id)
      loadData()
      ElMessage({
        type: 'success',
        message: '删除成功',
      })
    })
    .catch(action => {
      if (action === 'cancel')
        ElMessage({
          type: 'success',
          message: '取消删除',
        })
    })
}
</script>

<style>
.post-div {
  background-color: #fff;
  height: 50px;
}
.el-button {
  margin-top: 12px;
  margin-left: 10px;
}
</style>
